from pymongo import MongoClient
import pymongo
from pymongo.collection import Collection
from crawler.cfg import *

ITEM_ID = "item_id"
ITEM_SELLER_ID = "seller_id"
ITEM_BRAND = "brand"
ITEM_MODEL = "model"
ITEM_PRICE = "price"
ITEM_LAST_COMMENT_TIME = "lastCommentTime"
COMMENT_ID = "id"

MONGO = MongoClient(get_core_cfg('mongo.url'))


class CrawlerMongoClient:
    __mongo_db = None
    __item_table = None
    __comment_table = None

    def __init__(self, _type, mongo_db):
        self.__mongo_db = mongo_db
        self.__item_table = self.__mongo_db[_type + "_item"]
        self.__item_table.create_index([(ITEM_ID, pymongo.ASCENDING), (ITEM_SELLER_ID, pymongo.ASCENDING)], unique=True)
        self.__comment_table = self.__mongo_db[_type + "_comment"]
        self.__comment_table.create_index(
            [(ITEM_ID, pymongo.ASCENDING), (ITEM_SELLER_ID, pymongo.ASCENDING), (COMMENT_ID, pymongo.ASCENDING)],
            unique=True)
        self.__comment_table.create_index(
            [(ITEM_ID, pymongo.ASCENDING), (ITEM_SELLER_ID, pymongo.ASCENDING)])
        self.__comment_table.create_index(
            [(ITEM_ID, pymongo.ASCENDING)])
        self.__comment_table.create_index(
            [(COMMENT_ID, pymongo.ASCENDING)])

    def item_table(self) -> Collection:
        return self.__item_table

    def comment_table(self) -> Collection:
        return self.__comment_table

    def insert_item(self, item_id, seller_id="", brand="", model=""):
        if item_id is None:
            return False
        if self.item_table().count_documents({ITEM_ID: item_id, ITEM_SELLER_ID: seller_id}) == 0:
            item_map = {ITEM_ID: item_id, ITEM_SELLER_ID: seller_id, ITEM_BRAND: brand, ITEM_MODEL: model}
            self.item_table().insert_one(item_map)
            return True
            # print(item_id + "_" + seller_id)
        # else:
        return False

    def get_all_items(self):
        all_items_find = self.item_table().find({'skip': {'$ne': True}},
                                                no_cursor_timeout=True)
        all_items = []
        for item in all_items_find:
            all_items.append(item)
        return all_items

    def insert_comment(self, item_id, detail, seller_id="", comment_id=None):
        if item_id is None or detail is None:
            return False
        if comment_id is None:
            comment_id = detail[COMMENT_ID]
            if comment_id is None or comment_id == "":
                return False
        self.comment_table().find()
        detail[ITEM_ID] = item_id
        detail[ITEM_SELLER_ID] = seller_id
        detail[COMMENT_ID] = comment_id
        if self.comment_table().count_documents(
                {ITEM_ID: item_id, ITEM_SELLER_ID: seller_id, COMMENT_ID: comment_id}) == 0:
            self.comment_table().insert_one(detail)
        else:
            self.comment_table().update_one({ITEM_ID: item_id, ITEM_SELLER_ID: seller_id, COMMENT_ID: comment_id},
                                            {"$set": detail})
        return True

print(MONGO['jd'].list_collection_names())